/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Mar-04-2024 16:25:54
*
*/


#ifndef __key_H__
#define __key_H__




#ifdef __cplusplus
extern "C" {
#endif


#include <stdint.h>
#include "log.h"


typedef enum {
    KEY_State_0x00 = 0x00,
    KEY_State_0x01 = 0x01,
    KEY_State_0x02 = 0x02,
    KEY_State_0x03 = 0x03,
    KEY_State_0x04 = 0x04,
    KEY_State_0x05 = 0x05,
    KEY_State_0x06 = 0x06,
    KEY_State_0x07 = 0x07,
    KEY_State_0x08 = 0x08,
    KEY_State_0x09 = 0x09,
    KEY_State_0x0A = 0x0A,
    KEY_State_0x0B = 0x0B,
} KEY_zTypeDef;

#define     _KEY_FIFO_MASK      0x0f
#define     _KEY_FIFO_LEN       (_KEY_FIFO_MASK + 1)

struct KEY_CTRL {
    unsigned char  flag;
    unsigned char  key_code;
    unsigned char  head;
    unsigned char  tail;
    unsigned char  dat[_KEY_FIFO_LEN];
};

#define KEY_WAIT_TIMEROUT 10*4
extern uint8_t key_timeout;
typedef enum {
    MSG_DISPLAY_BLINK = 0x10,
    MSG_NUM_ERROR,
    MSG_ENTER_MENU,
    MSG_LOGO,
    MSG_ADC_UPDATE,//0x14
    MSG_CHARGING,
    MSG_BAT_NOREADY,//0x16
    MSG_BAT_LOW,//0x17
    MSG_BAT_READY,//0x18
    KEY_1 = 0x21,
    KEY_2,
    KEY_3,
    KEY_4,
    KEY_5,
    KEY_6,
    KEY_7,
    KEY_8,
    KEY_UP,//0x29
    KEY_DOWN,//0x2a
    KEY_POWER,
    KEY_RIGHT,
    KEY_MUL,
    KEY_HOME,//left up
    KEY_SET,//left down
    KEY_1_LONG = 0x61,
    KEY_2_LONG,
    KEY_3_LONG,
    KEY_4_LONG,
    KEY_5_LONG,
    KEY_6_LONG,
    KEY_7_LONG,
    KEY_8_LONG,
    KEY_UP_LONG,//0x69
    KEY_DOWN_LONG,//0x6a
    KEY_POWER_LONG,
    KEY_RIGHT_LONG,
    KEY_MUL_LONG,
    KEY_HOME_LONG,
    KEY_SET_LONG,
    KEY_TIMEOUT = 0x70,
    M_SEND_UPDATE = 0x81,
    M_SEND_TIMEOUT,
    M_SEND_END,
    M_SEND_START,
    M_ROTATE_START,
    M_ROTATE_TIMEOUT,//0x86
    M_ROTATE_UPDATE,
    M_ROTATE_UPDATE_2,
    M_ROTATE_STOP,
    M_SEND_UPDATE_2,
    BLE_TEST,
    BLE_PAUSE,
    BLE_ADD_ONCE,
    BLE_UPDATE_FIRMWARE,
    BLE_UPDATE_FIRMWARE_END,
    OP_PAUSE,
    UART_TEST,



} KEY_MSG;

//gen KEY_MSG by py
#ifdef DEBUG_VERSION
#define LOG_ENUM_KEY_MSG 1
#else
#define LOG_ENUM_KEY_MSG 0

#endif

//gen KEY_MSG by py

#define ENUM_KEY_MSG_NUM 57
typedef struct ENUM_KEY_MSG {
    KEY_MSG enum_index;
    char* enum_name_p;
} ENUM_KEY_MSG_TYPE;
char* print_enum_key_msg(KEY_MSG enum_index);
// gen end





typedef enum {
    BOOT_IMG,
    IDLE,
    SCR_SET_TOTAL_1,
    SCR_SET_SEND_MODE_2,
    SCR_SET_BOTTOM_CARD_3,
    SCR_SET_OTHER_4,
    SCR_COUNT_CARD,//6
    SCR_SEND_CARD,//7
    SCR_SET_NORMAL,
    SCR_SET_L,
    SCR_TEST_MODE,
    SCR_CHARGING,
    SYS_UPDATE,
    STOP_USE_ADC,

} MENU_SETTING;

typedef struct {

    uint8_t screen_index;
    uint8_t line;
    uint8_t key_ok;
    uint8_t blink;      //0-1:bllink 2:no blink

} Key_flag;



extern uint8_t key_led;
extern uint8_t pre_key_led;
extern volatile Key_flag key_flag;
extern uint8_t key_no_buzzer;
extern KEY_zTypeDef key_state;
//extern uint8_t key1_down;
extern uint8_t blink_Flag;
void people_mode_2_dir(void);
void people_mode_2_dir_texas(void);
void update_setting_num(void);

void set_total_screen_1(uint8_t line);
void set_send_mode_2(uint8_t line);
void set_bottom_card_3(uint8_t line);
void send_position_normal_1_3(uint8_t normal);
void send_position_do_1_3(uint8_t up);

void set_other_4(uint8_t line);

void people_normal_1_1(uint8_t normal);
void people_do_1_1(uint8_t up);

void con_mode_normal_2_1(uint8_t normal);
void con_mode_do_2_1(uint8_t up);

void set_timerout(uint8_t timeout);

void remain_mode_normal_3_1(uint8_t normal);
void remain_mode_do_3_1(uint8_t up);
void remain_num_normal_3_2(uint8_t normal);
void remain_num_do_3_2(uint8_t up);
void remain_click_send_normal_3_3(uint8_t normal);
void remain_click_send_do_3_3(uint8_t up);

void dingzhuang_normal_4_1(uint8_t normal);
void dingzhuang_do_4_1(uint8_t up);
void rotate_normal_4_2(uint8_t normal);
void    rotate_do_4_2(uint8_t up);
void language_normal_4_3(uint8_t normal);
void   language_do_4_3(uint8_t up);

void total_normal_1_2(uint8_t normal);
void total_do_1_2(uint8_t up);


void con_send_num_normal_2_2(uint8_t normal);
void con_send_num_do_2_2(uint8_t up);

void count_normal_2_3(uint8_t normal);
void count_do_2_3(uint8_t up);

void total_bai_normal(uint8_t normal);
void total_shi_normal(uint8_t normal);
void total_ge_normal(uint8_t normal);



void key_init(void);
void wait_key_release();

void key_init_first(void);

void key_task(void);
void key_deinit(void);
//unsigned char key_out(unsigned char*  item);



//#define DEBUG_KEY_IN_HOOK
#ifdef DEBUG_KEY_IN_HOOK
#warning("DEBUG_KEY_IN_HOOK !!!");
void key_in_hook(unsigned char key_code, uint16_t line,char *func);
#define key_in(a) key_in_hook(a, __LINE__,__func__)
unsigned char key_out_hook(unsigned char*  item, uint16_t line,char *func);

#define key_out(a) key_out_hook(a,__LINE__,__func__)

#else

void key_in(unsigned char key_code);
unsigned char key_out(unsigned char*  item);

#endif

void key_clear(void);



void do_back_key(void);
void do_blink(void);

void do_ok_key(void);
void do_up_key(void);
void do_down_key(void);


#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
class key
{

public:
    key();
    ~key();

};
#endif



#endif
